package com.fei.mongodb;

import org.bson.Document;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongodbTest {
	public static void main(String args[]) {
		try {
			// 连接到 mongodb 服务
			MongoClient mongoClient = new MongoClient("localhost", 27017);

			// 连接到数据库
			MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
			System.out.println("Connect to database successfully");
			MongoCollection<Document> docs = mongoDatabase.getCollection("stu");
			for(Document doc : docs.find()){
				MovieInfo info = JSONObject.parseObject(doc.toJson(), MovieInfo.class);
				
				System.out.println(info);
				break;
			}
			
		} catch (Exception e) {
			System.err.println(e.getClass().getName() + ": " + e.getMessage());
		}
	}
}
